package com.lmaos.allcontrol.beancontrol.utils_;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class NumberUtils {
	public static void main(String[] args) {
//		System.out.println(bytesToInt(intToBytes(Integer.MAX_VALUE)));
//		int[] is = toInt_copy(new int[]{1,2,3,4,5},new int[]{6,7,8,9,10},new int[]{11,12,13,14,15});
//		is = toRandom(is);
//		for (int i : is) {
//			System.out.println(i);
//		}
//		int x = 0;
//		for (int i = 0; i < 100000; i++) {
//			if(ok(0.001)){
//				x++;
//			}
//		}
//		System.out.println(x);
//		
//		List<Integer> i1 = Arrays.asList(12,3,4,5);
//		List<Integer> i2 = Arrays.asList(12,2,32,5,2);
//		List<Integer> i3 = Arrays.asList(7,3,1,51,9);
		
//		System.out.println(intersection(i1,i2));
		
		Object d = 3;
		System.out.println(toDoubleByObject(d, 0));
		
	}
	public static byte[] intToBytes(int value){
		byte[] src = new byte[4];  
	    src[0] = (byte) ((value>>24)&0xFF);  
	    src[1] = (byte) ((value>>16)&0xFF);  
	    src[2] = (byte) ((value>>8)&0xFF);    
	    src[3] = (byte) (value&0xFF);       
	    return src;  
	}
	public static int byteToInt(int b){
		if(b<0){
			return 256+b;
		}
		return b;
	}
	/** 4字节 byte[4] 转化为 int长度
	 * 
	 * @param bs
	 * @return
	 */
	public static int bytesToInt(byte[] src){
		int value;    
	    value = (int) ( ((src[0] & 0xFF)<<24)  
	            |((src[1] & 0xFF)<<16)  
	            |((src[2] & 0xFF)<<8)  
	            |(src[3] & 0xFF));  
	    return value;
		
	}
	
	public static int toInt(String src,int def){
		int v = def;
		if(src!=null&&!src.trim().isEmpty())
			try {
				v = Integer.parseInt(src);
			} catch (Exception e) {
			}
	
		return v;
	}
	public static int toIntByObject(Object src,int def){
		if(src == null||src.equals("")){
			return def;
		}
		if(src instanceof Integer){
			return (Integer)src;
		}else if(src instanceof Long){
			return (int)(long)(Long)src;
		}else if(src instanceof Double){
			return (int)(double)(Double)src;
		}else if(src instanceof Float){
			return (int)(float)(Float)src;
		}else if(src instanceof Short){
			return (Short)src;
		}
		int v = def;
		String src_s = String.valueOf(src);
		try {
			v = Integer.parseInt(src_s);
		} catch (Exception e) {
		}
		
		return v;
	}
	public static long toLongByObject(Object src,long def){
		if(src == null||src.equals("")){
			return def;
		}
		if(src instanceof Integer){
			return (Integer)src;
		}else if(src instanceof Long){
			return (Long)src;
		}else if(src instanceof Double){
			return (long)(double)(Double)src;
		}else if(src instanceof Float){
			return (long)(float)(Float)src;
		}else if(src instanceof Short){
			return (Short)src;
		}
		long v = def;
		String src_s = String.valueOf(src);
		try {
			v = Integer.parseInt(src_s);
		} catch (Exception e) {
		}
		
		return v;
	}
	
	public static double toDoubleByObject(Object src,double def){
		if(src == null||src.equals("")){
			return def;
		}
		if(src instanceof Integer){
			return (Integer)src;
		}else if(src instanceof Long){
			return (Long)src;
		}else if(src instanceof Double){
			return (Double)src;
		}else if(src instanceof Float){
			return (Float)src;
		}else if(src instanceof Short){
			return (Short)src;
		}
		double v = def;
		String src_s = String.valueOf(src);
		try {
			v = Integer.parseInt(src_s);
		} catch (Exception e) {
		}
		
		return v;
	}
	
	public static byte toByte(String src,byte def){
		byte v = def;
		if(src!=null&&!src.trim().isEmpty())
			try {
				v = Byte.parseByte(src);
			} catch (Exception e) {
			}
		
		return v;
	}
	
	public static short toShort(String src,short def){
		short v = def;
		if(src!=null&&!src.trim().isEmpty())
			try {
				v = Short.parseShort(src);
			} catch (Exception e) {
			}
		
		return v;
	}
	
	public static long toLong(String src,long def){
		long v = def;
		if(src!=null&&!src.trim().isEmpty())
			try {
				v = Long.parseLong(src);
			} catch (Exception e) {
			}
		
		return v;
	}
	
	public static float toFloat(String src,float def){
		float v = def;
		if(src!=null&&!src.trim().isEmpty())
			try {
				v = Float.parseFloat(src);
			} catch (Exception e) {
			}
		
		return v;
	}
	public static double toDouble(String src,double def){
		double v = def;
		if(src!=null&&!src.trim().isEmpty())
			try {
				v = Double.parseDouble(src);
			} catch (Exception e) {
			}
		
		return v;
	}
	
	public static boolean toBoolean(String src){
		return Boolean.parseBoolean(src);
	}
	
	public static int[] toRandom(int[] nums){
		Random random = new Random();
		Set<Integer> indexs = new HashSet<Integer>();
		int length = nums.length;
		int[] new_nums = new int[length];
		
		for (int i = 0; i < length; i++) {
			int index = random.nextInt(length);
			
			if(indexs.contains(index)){
				i--;
			}else{
				indexs.add(index);
				new_nums[index] = nums[i];
			}
		}
		return new_nums;
	}
	
	/** 获取一个数组 最小值 min 到 最大值 max 
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int[] toInts(int min,int max){
		int[] nums = new int[max-min+1];
		for (int i = 0; i < nums.length; i++,min++) {
			nums[i] = min;
		}
		return nums;
	}
	
	/** 将数组集合合成一个数组
	 * 
	 * @param nums_s
	 * @return
	 */
	public static int[] toInt_copy(int[]... nums_s){
		int length = 0;
		for (int[] is : nums_s) {
			length += is.length;
		}
		int[] des_muns = new int[length];
		int index = 0;
		for (int[] src : nums_s) {
			System.arraycopy(src, 0, des_muns, index, src.length);
			index+= src.length;
		}
		return des_muns;
	}
	
	public static boolean ok(double rate){
		if(rate>=1){
			return true;
		}else if(rate<=0){
			return false;
		}
		
		int x = 1000000000;
		int y = (int) (x*rate);
		Random r = new Random();
		int z = r.nextInt(x);
		
		return z<y;
	}
	
	/** 交集 集合中共同存在的值
	 * 
	 * @param nums_s
	 * @return
	 */
	@SafeVarargs
	public static Set<Integer> intersection(Collection<Integer>... nums_s){
		if(nums_s == null){
			return null;
		}
		if(nums_s.length == 0){
			return new HashSet<Integer>();
		}
		if(nums_s.length == 1){
			return new HashSet<Integer>(nums_s[0]);
		}
		
		Set<Integer> result = new HashSet<Integer>();
		Set<Integer>[] is_s = new HashSet[nums_s.length];
		for (int i = 0; i < is_s.length; i++) {
			is_s[i] = new HashSet<Integer>(nums_s[i]);
		}
		ex:for (int value : is_s[0]) {
			
			for (int i = 1; i < is_s.length; i++) {
				Set<Integer> is = is_s[i];
				if(!is.contains(value)){
					continue ex;
				}
			}
			result.add(value);
		}
		
		return result;
	}
	
	public static int getNextIndex(int now_index,int maxLength){
		now_index++;
		if(now_index>=maxLength){
			now_index = 0;
		}
		return now_index;
	}
	
	
}
